import { AdManager, ReportType } from "./ads/AdManager";
import { ANSWER_STATE_ENUM, BUNDLE_TYPE_ENUM, EVENT_NAME_ENUM } from "./base/EnumIndex";
import { Tools } from "./common/Tools";
import GoalItem from "./item/GoalItem";
import { ProgressItem } from "./item/ProgressItem";
import SliderItem from "./item/SliderItem";
import StepItem from "./item/StepItem";
import TimeItem from "./item/TimeItem";
import { AudioManager1 } from "./manager/AudioManager1";
import { EventManager } from "./manager/EventManager";
import { GameManager } from "./manager/GameManager";
import { LevelManager } from "./manager/LevelManager";
import { UIManager } from "./manager/UIManager";

const { ccclass, property } = cc._decorator;

/** 关卡公用UI */
@ccclass
export default class LevelPre_Common extends cc.Component {
    @property(cc.Node)
    mask: cc.Node = null;
    @property(cc.Label)
    title: cc.Label = null;

    @property(TimeItem)
    timeItem: TimeItem = null;
    @property(cc.Node)
    timeBtn: cc.Node = null;
    @property(SliderItem)
    timeSlider: SliderItem = null;
    @property(ProgressItem)
    progressItem: ProgressItem = null;

    @property(StepItem)
    stepItem: StepItem = null;
    @property(cc.Node)
    stepBtn: cc.Node = null;

    @property(GoalItem)
    goalItem: GoalItem = null;

    @property(cc.Node)
    blockPanel: cc.Node = null;

    @property(cc.Node)
    commitBtn: cc.Node = null;

    @property(cc.Node)
    shareBtn: cc.Node = null;

    @property(cc.Node)
    tipVideoIcon: cc.Node = null;

    @property(cc.Node)
    lbLvName: cc.Node = null;

    @property(cc.Node)
    lbLvNameBg: cc.Node = null;

    interJson: any;
    /**30秒无操作 开始引导提示 */
    private noTouchTime: number = 30;
    private timeoutNum: number = null;
    private timeoutNum2: number = null;
    private guideTipNode: cc.Node = null;
    onLoad() {
        EventManager.ins.on(EVENT_NAME_ENUM.ENABLE_CLICK, this.enableClick, this);
        EventManager.ins.on(EVENT_NAME_ENUM.OPEN_TIP_UI, this.openTipUI, this);
        EventManager.ins.on(EVENT_NAME_ENUM.RESET_GUIDE_TIP_TIME, this.startTimePlayGuideTip, this);


        this.initUI();
    }
    onDestroy() {
        EventManager.ins.off(EVENT_NAME_ENUM.ENABLE_CLICK, this.enableClick, this);
        EventManager.ins.off(EVENT_NAME_ENUM.OPEN_TIP_UI, this.openTipUI, this);
        EventManager.ins.off(EVENT_NAME_ENUM.RESET_GUIDE_TIP_TIME, this.startTimePlayGuideTip, this);
    }
    initUI() {
        this.title.string = "";

        this.timeItem.node.active = false;
        this.timeBtn.active = false;
        this.timeSlider.node.active = false;
        this.progressItem.node.active = false;

        this.stepItem.node.active = false;
        this.stepBtn.active = false;

        this.goalItem.node.active = false;

        this.commitBtn.active = false;

        this.lbLvName.active = this.lbLvNameBg.active = false;
        let isShowLvName = Tools.getStorage('setShowLvNameActive');
        if (isShowLvName && isShowLvName != "false") {
            this.lbLvName.active = this.lbLvNameBg.active = true;
            let name = LevelManager.ins.curLvInfo.name;
            let page = LevelManager.ins.getCurrentLevelPageIdx(6);
            this.lbLvName.getComponent(cc.Label).string = "第" + page + "页    " + name;
        }
    }
    start() {
        this.blockPanel.active = true;
        cc.tween(this.blockPanel).to(0.5, { opacity: 0 }).start();
        this.updateTipVideoIcon();
        this.updateTipBtn();

        AdManager.naoLiReportVideo(1, "提示");
        AdManager.naoLiReportVideo(1, "加时");
        AdManager.naoLiReportVideo(1, "加步数");

        this.startTimePlayGuideTip();

    }
    init(interJson: any) {
        this.interJson = interJson;
        this.initTitle();
        this.initTime();
        this.initStep();
        this.initGoalItem();
        this.initTimeSlider();
        this.initProgressItem();
        this.initCommitBtn();
        this.initShareBtn();
    }
    initTitle() {
        this.title.string = this.interJson.title;//标题
    }
    initTime() {
        // 时间
        this.timeItem.node.active = !this.interJson.removeTime;
        this.timeBtn.active = this.timeItem.node.active;
        if (this.interJson.downTimeNum) {
            this.timeItem.initTime(this.interJson.downTimeNum)
        } else {
            this.timeItem.initTime(90);
        }
    }
    initStep() {
        // 步数
        this.stepItem.node.active = this.interJson.totalStep;
        this.stepBtn.active = this.stepItem.node.active;
        if (this.interJson.totalStep) {
            this.stepItem.initStep(this.interJson.totalStep);
        }
    }
    /** 加载  游戏目标 */
    initGoalItem() {
        let showGoal = this.interJson.moneyTitle || this.interJson.moneyTitleStr;
        this.goalItem.node.active = showGoal;
        if (this.interJson.moneyTitleStr) {
            this.goalItem.initTitleLabel(this.interJson.moneyTitleStr);
            this.goalItem.initUnit(this.interJson.moneyUnit);
        } else if (this.interJson.moneyTitle) {
            this.goalItem.initTitleSpr(this.interJson.moneyTitle);
            this.goalItem.initUnit(this.interJson.moneyUnit);
        }
    }

    /** 时间进度条 */
    initTimeSlider() {
        this.timeSlider.node.active = this.interJson.sliderScore;
        if (this.interJson.sliderScore) {
            this.timeSlider.setProgress(1);
        }

        if (this.interJson.sliderHead) {
            this.timeSlider.updateHead(this.interJson.sliderHead);
        }
    }
    /** 满意度进度条 */
    initProgressItem() {
        let length = 0;
        if (this.interJson.winStates)
            length = this.interJson.winStates.length;
        this.progressItem.node.active = this.interJson.showPictureProgress;
        this.progressItem.setTotal(length);
        if (this.interJson.progressTitle) {
            this.progressItem.setTitle(this.interJson.progressTitle);
        }
    }
    /** 提交按钮 */
    initCommitBtn() {
        this.commitBtn.active = this.interJson.commitButton;
    }

    /**分享按钮 */
    initShareBtn() {
        this.shareBtn.active = AdManager.isShowShareBtn();
    }

    updateTipVideoIcon() {
        this.tipVideoIcon.active = LevelManager.ins.getAnswerState() === ANSWER_STATE_ENUM.LOCKED;
    }

    updateTipBtn() {
        if (LevelManager.ins.curKeyInfo["keyStepInfo"] || LevelManager.ins.curKeyInfo["keyProgressInfos"] || LevelManager.ins.curKeyInfo["keyProgressInfos2"]) {
            this.node.getChildByName("tipStepBtn").active = true;
            this.node.getChildByName("tipBtn").active = false;
        } else {
            this.node.getChildByName("tipStepBtn").active = false;
            this.node.getChildByName("tipBtn").active = true;
        }
    }
    /** 点击返回按钮 */
    clickBtnReturn() {
        AudioManager1.playClick();
        let lvName = LevelManager.ins.getBundleName();
        if (lvName == "15_268") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_268")
            level15.pauseAni();
        }
        if (lvName == "15_271") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_271")
            level15.pauseAni();
        }
        if (lvName == "15_272") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_272")
            level15.pauseAni();
        }
        if (lvName == "15_279") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_279")
            level15.pauseAni();
        }
        if (lvName == "15_281") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_281")
            level15.pauseAni();
        }
        if (lvName == "15_287") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_287")
            level15.pauseAni();
        }
        if (lvName == "15_286") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_286")
            level15.pauseAni();
        }
        if (lvName == "15_288") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_288")
            level15.pauseAni();
        }
        if (lvName == "15_289") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_289")
            level15.pauseAni();
        }
        if (lvName == "15_299") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_299")
            level15.pauseAni();
        }
        if (lvName == "15_301") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_301")
            level15.pauseAni();
        }
        if (lvName == "15_302") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_302")
            level15.pauseAni();
        }
        if (lvName == "15_304") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_304")
            level15.pauseAni();
        }
        if (lvName == "15_305") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_305")
            level15.pauseAni();
        }
        if (lvName == "15_308") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_308")
            level15.pauseAni();
        }
        if (lvName == "15_307") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_307")
            level15.pauseAni();
        }
        if (lvName == "15_290") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_290")
            level15.pauseAni();
        }
        if (lvName == "15_309") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_309")
            level15.pauseAni();
        }
        if (lvName == "15_317") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_317")
            level15.pauseAni();
        }
        // AudioManager.stopAllAudio();
        // UIManager.ins.closeAll();
        // UIManager.ins.showUI("LevelUI", BUNDLE_TYPE_ENUM.HOME);

        UIManager.ins.showPop("BackUI", BUNDLE_TYPE_ENUM.GAME);
    }
    /** 点击暂停按钮 */
    clickBtnPause() {
        AudioManager1.playClick();
        UIManager.ins.showPop("PauseUI", BUNDLE_TYPE_ENUM.GAME);
    }
    /** 点击提示按钮 */
    clickBtnTip() {
        EventManager.ins.emit(EVENT_NAME_ENUM.PAUSE_TIME);
        AudioManager1.playClick();
        let answerState = LevelManager.ins.getAnswerState();
        this.startTimePlayGuideTip();
        if (answerState === ANSWER_STATE_ENUM.LOCKED) {
            UIManager.ins.enabledClick(false);
            let idx = LevelManager.ins.getLvInfoIdx(LevelManager.ins.curLvInfo.type, LevelManager.ins.curLvInfo.lv) + 1;
            AdManager.reportVideo(1, idx, LevelManager.ins.curLvInfo.name);
            AdManager.naoLiReportVideo(2, "提示");
            AdManager.showVideoAd(() => {
                UIManager.ins.enabledClick(true);
                AdManager.reportOverVideo(idx, LevelManager.ins.curLvInfo.name);
                AdManager.naoLiReportVideo(3, "提示");
                AdManager.naoLiReportVideo(4, "提示");
                LevelManager.ins.unlockLevelTip1();
                UIManager.ins.showPop("TipUI", BUNDLE_TYPE_ENUM.GAME, LevelManager.ins.curKeyInfo);
                this.updateTipVideoIcon();
            }, () => {
                EventManager.ins.emit(EVENT_NAME_ENUM.RESUME_TIME);
                UIManager.ins.enabledClick(true);
            });
        } else {
            UIManager.ins.showPop("TipUI", BUNDLE_TYPE_ENUM.GAME, LevelManager.ins.curKeyInfo);
        }
    }

    /**点击步骤提示按钮 */
    clickBtnStepTip() {
        //暂停
        let lvName = LevelManager.ins.getBundleName();
        if (lvName == "15_268") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_268")
            level15.pauseAni();
        }
        if (lvName == "15_271") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_271")
            level15.pauseAni();
        }
        if (lvName == "15_272") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_272")
            level15.pauseAni();
        }
        if (lvName == "15_279") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_279")
            level15.pauseAni();
        }
        if (lvName == "15_281") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_281")
            level15.pauseAni();
        }
        if (lvName == "15_287") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_287")
            level15.pauseAni();
        }
        if (lvName == "15_286") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_286")
            level15.pauseAni();
        }
        if (lvName == "15_288") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_288")
            level15.pauseAni();
        }
        if (lvName == "15_289") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_289")
            level15.pauseAni();
        }
        if (lvName == "15_299") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_299")
            level15.pauseAni();
        }
        if (lvName == "15_301") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_301")
            level15.pauseAni();
        }
        if (lvName == "15_302") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_302")
            level15.pauseAni();
        }
        if (lvName == "15_304") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_304")
            level15.pauseAni();
        }
        if (lvName == "15_305") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_305")
            level15.pauseAni();
        }
        if (lvName == "15_308") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_308")
            level15.pauseAni();
        }
        if (lvName == "15_307") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_307")
            level15.pauseAni();
        }
        if (lvName == "15_290") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_290")
            level15.pauseAni();
        }
        if (lvName == "15_309") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_309")
            level15.pauseAni();
        }
        if (lvName == "15_317") {
            let level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren("Level_15_317")
            level15.pauseAni();
        }
        EventManager.ins.emit(EVENT_NAME_ENUM.PAUSE_TIME);
        AudioManager1.playClick();
        let idx = LevelManager.ins.getLvInfoIdx(LevelManager.ins.curLvInfo.type, LevelManager.ins.curLvInfo.lv) + 1;
        AdManager.reportVideo(1, idx, LevelManager.ins.curLvInfo.name);
        AdManager.naoLiReportVideo(2, "提示");
        this.startTimePlayGuideTip();
        UIManager.ins.enabledClick(false);
        AdManager.showVideoAd(() => {
            UIManager.ins.enabledClick(true);
            AdManager.reportOverVideo(idx, LevelManager.ins.curLvInfo.name);
            AdManager.naoLiReportVideo(3, "提示");
            AdManager.naoLiReportVideo(4, "提示");
            UIManager.ins.showPop("TipUI", BUNDLE_TYPE_ENUM.GAME, LevelManager.ins.curKeyInfo);
        }, () => {
            EventManager.ins.emit(EVENT_NAME_ENUM.RESUME_TIME);
            UIManager.ins.enabledClick(true);
        });
    }

    enableClick(enabled: boolean) {
        this.mask.active = !enabled;
    }

    /** 点击加时按钮 */
    clickBtnAddTime() {
        AudioManager1.playClick();
        this.timeItem.pauseTime();
        AdManager.umengEvent(ReportType.CLICK_ADS, { tag: "广告开始", name: LevelManager.ins.curLvInfo.name, value: "加时" });
        let idx = LevelManager.ins.getLvInfoIdx(LevelManager.ins.curLvInfo.type, LevelManager.ins.curLvInfo.lv) + 1;
        AdManager.reportVideo(4, idx, LevelManager.ins.curLvInfo.name);
        AdManager.naoLiReportVideo(2, "加时");
        UIManager.ins.enabledClick(false);
        AdManager.showVideoAd(() => {
            UIManager.ins.enabledClick(true);
            AdManager.reportOverVideo(idx, LevelManager.ins.curLvInfo.name);
            AdManager.naoLiReportVideo(3, "加时");
            AdManager.naoLiReportVideo(4, "加时");
            this.timeItem.addTime();
        }, () => {
            UIManager.ins.enabledClick(true);
            this.timeItem.startTime()
        });
    }
    /** 点击加步数按钮 */
    clickBtnAddStep() {
        AudioManager1.playClick();
        AdManager.umengEvent(ReportType.CLICK_ADS, { tag: "广告开始", name: LevelManager.ins.curLvInfo.name, value: "增加步数" });
        let idx = LevelManager.ins.getLvInfoIdx(LevelManager.ins.curLvInfo.type, LevelManager.ins.curLvInfo.lv) + 1;
        AdManager.reportVideo(6, idx, LevelManager.ins.curLvInfo.name);
        AdManager.naoLiReportVideo(2, "加步数");
        UIManager.ins.enabledClick(false);
        AdManager.showVideoAd(() => {
            UIManager.ins.enabledClick(true);
            AdManager.reportOverVideo(idx, LevelManager.ins.curLvInfo.name);
            AdManager.naoLiReportVideo(3, "加步数");
            AdManager.naoLiReportVideo(4, "加步数");
            AdManager.umengEvent(ReportType.CLICK_ADS, { tag: "广告完成", name: LevelManager.ins.curLvInfo.name, value: "增加步数" });
            this.stepItem.addStep();
        }, () => {
            UIManager.ins.enabledClick(true);
        });
    }
    /** 点击提交按钮 */
    clickBtnCommit() {
        UIManager.ins.enabledClick(false);

        this.timeItem.stopTime();
        EventManager.ins.emit(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, "updateIsWin");
        AudioManager1.playAudio("game15_tijiao", 1, BUNDLE_TYPE_ENUM.GAME);
    }
    /** 点击跳过本关 */
    clickBtnPass() {
        AudioManager1.playClick();
        GameManager.ins.passLevel();
    }

    /** 分享 */
    clickBtnShare() {
        AudioManager1.playClick();
        AdManager.shareFriends();
    }

    openTipUI(isOpen: boolean) {
        if (isOpen) {
            EventManager.ins.emit(EVENT_NAME_ENUM.PAUSE_TIME);
        } else {
            EventManager.ins.emit(EVENT_NAME_ENUM.RESUME_TIME);
        }
    }

    startTimePlayGuideTip() {
        this.isPlayGuideTip(false);
        //开启自动答题不弹点我提示
        let isAutoAnswerActive = Tools.getStorage('setAutoAnswerActive');
        if (isAutoAnswerActive && isAutoAnswerActive != "false") {
            return;
        }

        let isAnswerRightActive = Tools.getStorage('setAnswerRightActive')
        if (isAnswerRightActive && isAnswerRightActive != "false") {
            return;
        }

        if (this.timeoutNum) {
            clearTimeout(this.timeoutNum);
            this.timeoutNum = null;
        }
        this.timeoutNum = setTimeout(() => {
            this.isPlayGuideTip(true);
        }, this.noTouchTime * 1000);
    }

    isPlayGuideTip(active) {
        if (this.node) {
            if (!this.guideTipNode) {
                let type = Tools.random(1, 2);
                this.guideTipNode = this.node.getChildByName("guideTip" + type);
                this.guideTipNode.active = active;
            } else {
                this.guideTipNode.active = active;
            }
            if (active) {
                this.timeoutNum2 = setTimeout(() => {
                    this.startTimePlayGuideTip();
                }, 3 * 1000)
            } else {
                this.guideTipNode = null;
                if (this.timeoutNum2) {
                    clearTimeout(this.timeoutNum2);
                    this.timeoutNum2 = null;
                }
            }
        }
    }
}